<template>
  <div class="xtx-form">
    <Form
      ref="target"
      autocomplete="off"
      :validation-schema="rules"
      v-slot="{ errors }"
    >
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-user"></i>
          <Field
            v-model="form.account"
            class="input"
            type="text"
            placeholder="请输入用户名"
            name="account"
          />
        </div>
        <div class="error" v-if="errors.account">{{ errors.account }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            v-model="form.mobile"
            class="input"
            type="text"
            placeholder="请输入手机号"
            name="mobile"
          />
        </div>
        <div class="error" v-if="errors.mobile">{{ errors.mobile }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            v-model="form.code"
            class="input"
            type="text"
            placeholder="请输入验证码"
            name="code"
          />
          <span class="code" @click="send">{{
            time === 0 ? '发送验证码' : `${time}s后发送`
          }}</span>
        </div>
        <div class="error" v-if="errors.code">{{ errors.code }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            v-model="form.password"
            class="input"
            type="password"
            placeholder="请输入密码"
            name="password"
          />
        </div>
        <div class="error" v-if="errors.password">{{ errors.password }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            v-model="form.rePassword"
            class="input"
            type="password"
            placeholder="请确认密码"
            name="rePassword"
          />
        </div>
        <div class="error" v-if="errors.rePassword">
          {{ errors.rePassword }}
        </div>
      </div>
    </Form>
    <a href="javascript:;" class="submit" @click="bind">立即提交</a>
  </div>
</template>

<script>
import { reactive, ref } from 'vue'
import { Form, Field } from 'vee-validate'
import { account, mobile, code, password, rePassword } from '@/utils/validate'
import { useCountDown } from '@/hooks'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { Message } from '@/components'
export default {
  name: 'CallbackPatch',
  components: { Form, Field },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const form = reactive({
      account: null,
      mobile: null,
      code: null,
      password: null,
      rePassword: null
    })
    const rules = {
      account,
      mobile,
      code,
      password,
      rePassword
    }
    const { time, start } = useCountDown()
    const target = ref(null)
    const send = async () => {
      if (time.value > 0) return
      // 校验手机号
      const msg = mobile(form.mobile)
      if (msg !== true) {
        target.value.setFieldError('mobile', msg)
        return
      }
      // 校验通过
      await userQQPatchCode(form.mobile)
      // 开启倒计时
      start()
    }
    const store = useStore()
    const router = useRouter()
    const bind = async () => {
      const valid = await target.value.validate()
      if (!valid) return
      const res = await userQQPatchLogin(props.unionId, form)
      Message({ text: '登录成功' })
      store.commit('user/setProfile', res.result)
      router.push('/')
    }
    return {
      form,
      rules,
      time,
      send,
      target,
      bind
    }
  }
}
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
